﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Controls.Interface;
using System.Globalization;

namespace Common.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MyTextBox runat=server></{0}:MyTextBox>")]
    public class MyTextBox : WebControl, ITextControl, IDisplayControl
    {
        private string _text = string.Empty;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                //String s = (String)ViewState["Text"];
                //return ((s == null) ? String.Empty : s);

                return Page.Request.Form[this.UniqueID];
            }

            set
            {
                _text = value;
                //ViewState["Text"] = value;
            }
        }

        public ControlDisplayType DisplayType
        {
            get
            {
                return (ViewState["DisplayType"] == null) ? ControlDisplayType.Edit : (ControlDisplayType)ViewState["DisplayType"];
            }
            set
            {
                ViewState["DisplayType"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                if (DisplayType == ControlDisplayType.View)
                    return HtmlTextWriterTag.Span;
                return HtmlTextWriterTag.Input;
            }
        }

        //protected override void AddAttributesToRender(HtmlTextWriter writer)
        //{
        //    Page page = this.Page;
        //    if (page != null)
        //    {
        //        page.VerifyRenderingInServerForm(this);
        //    }
        //    string uniqueID = this.UniqueID;
        //    if (uniqueID != null)
        //    {
        //        writer.AddAttribute(HtmlTextWriterAttribute.Name, uniqueID);
        //    }
        //    TextBoxMode textMode = this.TextMode;
        //    switch (textMode)
        //    {
                
        //        case TextBoxMode.SingleLine:
        //            {
        //                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
        //                if (((this.AutoCompleteType != AutoCompleteType.None) && (this.Context != null)) && (this.Context.Request.Browser["supportsVCard"] == "true"))
        //                {
        //                    if (this.AutoCompleteType == AutoCompleteType.Disabled)
        //                    {
        //                        writer.AddAttribute(HtmlTextWriterAttribute.AutoComplete, "off");
        //                    }
        //                    else if (this.AutoCompleteType == AutoCompleteType.Search)
        //                    {
        //                        writer.AddAttribute(HtmlTextWriterAttribute.VCardName, "search");
        //                    }
        //                    else if (this.AutoCompleteType == AutoCompleteType.HomeCountryRegion)
        //                    {
        //                        writer.AddAttribute(HtmlTextWriterAttribute.VCardName, "HomeCountry");
        //                    }
        //                    else if (this.AutoCompleteType == AutoCompleteType.BusinessCountryRegion)
        //                    {
        //                        writer.AddAttribute(HtmlTextWriterAttribute.VCardName, "BusinessCountry");
        //                    }
        //                    else
        //                    {
        //                        string str2 = Enum.Format(typeof(AutoCompleteType), this.AutoCompleteType, "G");
        //                        if (str2.StartsWith("Business", StringComparison.Ordinal))
        //                        {
        //                            str2 = str2.Insert(8, ".");
        //                        }
        //                        else if (str2.StartsWith("Home", StringComparison.Ordinal))
        //                        {
        //                            str2 = str2.Insert(4, ".");
        //                        }
        //                        writer.AddAttribute(HtmlTextWriterAttribute.VCardName, "vCard." + str2);
        //                    }
        //                }
        //                string text = this.Text;
        //                if (text.Length > 0)
        //                {
        //                    writer.AddAttribute(HtmlTextWriterAttribute.Value, text);
        //                }
        //                break;
        //            }
        //        case TextBoxMode.Password:
        //            writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
        //            break;
        //    }
        //    int maxLength = this.MaxLength;
        //    if (maxLength > 0)
        //    {
        //        writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, maxLength.ToString(NumberFormatInfo.InvariantInfo));
        //    }
        //    maxLength = this.Columns;
        //    if (maxLength > 0)
        //    {
        //        writer.AddAttribute(HtmlTextWriterAttribute.Size, maxLength.ToString(NumberFormatInfo.InvariantInfo));
        //    }
        //Label_024E:
        //    if (this.ReadOnly)
        //    {
        //        writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
        //    }
        //    if ((this.AutoPostBack && (page != null)) && page.ClientSupportsJavaScript)
        //    {
        //        string str4 = null;
        //        if (base.HasAttributes)
        //        {
        //            str4 = base.Attributes["onchange"];
        //            if (str4 != null)
        //            {
        //                str4 = Util.EnsureEndWithSemiColon(str4);
        //                base.Attributes.Remove("onchange");
        //            }
        //        }
        //        PostBackOptions options = new PostBackOptions(this, string.Empty);
        //        if (this.CausesValidation)
        //        {
        //            options.PerformValidation = true;
        //            options.ValidationGroup = this.ValidationGroup;
        //        }
        //        if (page.Form != null)
        //        {
        //            options.AutoPostBack = true;
        //        }
        //        str4 = Util.MergeScript(str4, page.ClientScript.GetPostBackEventReference(options, true));
        //        writer.AddAttribute(HtmlTextWriterAttribute.Onchange, str4);
        //        if (textMode != TextBoxMode.MultiLine)
        //        {
        //            string str5 = "if (WebForm_TextBoxKeyHandler(event) == false) return false;";
        //            if (base.HasAttributes)
        //            {
        //                string str6 = base.Attributes["onkeypress"];
        //                if (str6 != null)
        //                {
        //                    str5 = str5 + str6;
        //                    base.Attributes.Remove("onkeypress");
        //                }
        //            }
        //            writer.AddAttribute("onkeypress", str5);
        //        }
        //        if (base.EnableLegacyRendering)
        //        {
        //            writer.AddAttribute("language", "javascript", false);
        //        }
        //    }
        //    else if (page != null)
        //    {
        //        page.ClientScript.RegisterForEventValidation(this.UniqueID, string.Empty);
        //    }
        //    if (this.Enabled && !base.IsEnabled)
        //    {
        //        writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
        //    }
        //    base.AddAttributesToRender(writer);
        //}

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
    }
}
